//
//  PicsView.m
//  JmlLive2
//  自定义图片浏览视图
//  Created by 王伟 on 15/12/8.
//  Copyright © 2015年 王伟. All rights reserved.
//

#import "PicsView.h"

#import "UIImageView+WebCache.h"//图片加载

@interface PicsView()<UIScrollViewDelegate>
@property (strong,nonatomic) UIScrollView *myScrolV;//滚动视图对象
@property (strong,nonatomic) UIPageControl *pagecV;//滚动指示对象
@end

@implementation PicsView

#pragma mark 初始化方法
//初始化方法，参数是一组图片
-(instancetype) initWithImages:(NSArray*) images AtIndex:(NSInteger) index{
    //加载xib文件
    self = [super initWithFrame:CGRectZero];
    if(self){
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
        [self addGestureRecognizer:tap];
//        self =  [[[NSBundle mainBundle] loadNibNamed:@"PicsView" owner:nil options:nil] lastObject];
        
        self.myScrolV = [[UIScrollView alloc]init];
        self.backgroundColor=[UIColor blackColor];
        self.myScrolV.pagingEnabled=YES;//开启分页效果
        self.myScrolV.showsHorizontalScrollIndicator = NO;//隐藏指示条
        self.myScrolV.scrollEnabled=YES;
        self.myScrolV.bounces=NO;
        [self.myScrolV setContentOffset:CGPointMake(LLScreenWidth * index, 0)];
        
        self.pagecV = [[UIPageControl alloc]init];
        self.pagecV.numberOfPages=[images count];
        self.pagecV.currentPage=index;
        [self.pagecV addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        
        
        for(int i=0;i<[images count];i++){
            
            UIImageView *imgv1 = [[UIImageView alloc]init];
            imgv1.tag=i;
            imgv1.userInteractionEnabled=YES;
            imgv1.frame=CGRectMake(LLScreenWidth * i, 0, LLScreenWidth, LLScreenHeight);
            imgv1.contentMode=UIViewContentModeScaleAspectFit;
            imgv1.clipsToBounds = YES;
            id isImage = images[i];
            if ([isImage isKindOfClass:[UIImage class]]) {
                imgv1.image = isImage;
            }else{
            NSURL *imgPath1=[NSURL URLWithString:images[i]];
            [imgv1 sd_setImageWithURL:imgPath1 placeholderImage:[UIImage imageNamed:@"default"]];
            }
            [self.myScrolV addSubview:imgv1];
            
        }
        
        //按钮
//        UIButton *btn1 = [[UIButton alloc] init];
//        //[btn1 setTitle:@"返回" forState:UIControlStateNormal];
//        [btn1 setImage :[UIImage imageNamed:@"nav_back_icon"] forState:(UIControlStateNormal)];
//        [btn1 addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
//        btn1.imageEdgeInsets=UIEdgeInsetsMake(0,0,0,0);
//        btn1.frame=CGRectMake(5, 20, 50, 50);
//        btn1.backgroundColor = [UIColor blackColor];
//        btn1.alpha = 0.8;
//        [self addSubview:btn1];
        
        [self addSubview:self.myScrolV];
        [self addSubview:self.pagecV];
//        [self bringSubviewToFront:btn1];
        
//        UIButton *btn2 = [[UIButton alloc] init];
//        [btn2 setImage :[UIImage imageNamed:@"icon_btn_save"] forState:(UIControlStateNormal)];
//        [btn2 addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
//        btn2.imageEdgeInsets=UIEdgeInsetsMake(0,0,0,0);
//        btn2.frame=CGRectMake(kScreenW-55, 20, 50, 50);
//        [self addSubview:btn2];
//        [self bringSubviewToFront:btn2];
    }
    self.picIndex=index;
    self.imgAry=images;
   return self;
}

#pragma mark 设置布局
-(void)layoutSubviews{
    self.frame=CGRectMake(0, 0, LLScreenWidth, LLScreenHeight);
    self.myScrolV.frame=CGRectMake(0, 0, LLScreenWidth, LLScreenHeight);
    self.myScrolV.contentSize = CGSizeMake(LLScreenWidth*[self.imgAry count], 0);//滚动范围
    [self.myScrolV setContentOffset:CGPointMake(LLScreenWidth * self.picIndex, 0)];//当前滚动位置
    self.pagecV.frame=CGRectMake(0, LLScreenHeight-50, LLScreenWidth,20);
    self.myScrolV.delegate = self;

}

#pragma mark 显示视图
-(void)showView{
    //转场动画
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window] ;
    [UIView transitionWithView:window duration:0.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
       [window addSubview:self];
    } completion:^(BOOL finished) {
        
    }];
}
#pragma mark 关闭视图
//关闭视图
-(void)closeView{
    //缩放动画
    [UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        
        //CGAffineTransformScale ,第一个参数要缩放view ，后面参数缩放的比例
        self.transform=CGAffineTransformScale(self.transform, 0.2, 0.1);
        self.alpha=0.1;
    } completion:^(BOOL finished) {
        if (self.removeView) {
            self.removeView();
        }
        [self removeFromSuperview];
       
    }];
    //消失动画
//    [UIView transitionWithView:self duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
//        self.hidden=YES;
//    } completion:^(BOOL finished) {
//        [self removeFromSuperview];
//    }];
    
    /*
     UIViewAnimationOptionTransitionNone            //无转场动画
     
     UIViewAnimationOptionTransitionFlipFromLeft    //转场从左翻转
     
     UIViewAnimationOptionTransitionFlipFromRight   //转场从右翻转
     
     UIViewAnimationOptionTransitionCurlUp          //上卷转场
     
     UIViewAnimationOptionTransitionCurlDown        //下卷转场
     
     UIViewAnimationOptionTransitionCrossDissolve   //转场交叉消失
     
     UIViewAnimationOptionTransitionFlipFromTop     //转场从上翻转
     
     UIViewAnimationOptionTransitionFlipFromBottom  //转场从下翻转
     
     //时间函数曲线相关
     
     UIViewAnimationOptionCurveEaseInOut //时间曲线函数，由慢到快
     
     UIViewAnimationOptionCurveEaseIn //时间曲线函数，由慢到特别快
     
     UIViewAnimationOptionCurveEaseOut //时间曲线函数，由快到慢
     
     UIViewAnimationOptionCurveLinear //时间曲线函数，匀速
     */
    
}

#pragma mark 事件
//点击事件
- (IBAction)changePage:(id)sender {
//    CLog(@"%li",(long)self.pagecV.currentPage);
    self.picIndex = self.pagecV.currentPage;//获取当前pagecontroll的值
    [self.myScrolV setContentOffset:CGPointMake(LLScreenWidth * self.pagecV.currentPage, 0)];//根据pagecontroll的值来改变scrollview的滚动位置，以此切换到指定的页面
    
}
-(void)saveClick:(UIButton*)sender{
    NSInteger pageI = (self.myScrolV.contentOffset.x+10) / LLScreenWidth;
    UIImageView *imgV;
    for (UIView *uv in [self.myScrolV subviews]) {
        //CLog(@"%@ tag:%li",[uv class],uv.tag);
        if(uv.tag==pageI){
            imgV = (UIImageView*)uv;
            break;
        }
    }
   UIImageWriteToSavedPhotosAlbum(imgV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}
//滚动指示跟随
- (void)scrollViewDidScroll:(UIScrollView *)sender {
    self.picIndex =self.myScrolV.contentOffset.x / LLScreenWidth;//通过滚动的偏移量来判断目前页面所对应的小白点
    self.pagecV.currentPage = self.picIndex;//pagecontroll响应值的变化
}
//保存图片后的事件
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
  
    if(!error){
//        [PubHelper AlertMsg:@"图片保存完成" selfView:self];
        
    }else{
//        [PubHelper AlertMsg:@"保存出错" selfView:self];
    }
}

@end
